package com.cn.xiaonuo.tcrfinance.tcrshops.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cn.xiaonuo.common.entity.BusinessMessage;
import com.cn.xiaonuo.core.exception.ServiceException;
import com.cn.xiaonuo.core.factory.PageFactory;
import com.cn.xiaonuo.core.pojo.response.ResponseData;
import com.cn.xiaonuo.tcrfinance.sysmgmt.entity.PvSystemConfig;
import com.cn.xiaonuo.tcrfinance.sysmgmt.mapper.PvSystemConfigMapper;
import com.cn.xiaonuo.tcrfinance.tcrshops.entity.*;
import com.cn.xiaonuo.tcrfinance.tcrshops.mapper.*;
import com.cn.xiaonuo.tcrfinance.tcrshops.param.ProductReqParam;
import com.cn.xiaonuo.tcrfinance.tcrshops.param.PvProductParam;
import com.cn.xiaonuo.tcrfinance.tcrshops.service.MerchantBackService;
import com.cn.xiaonuo.tcrfinance.tcrshops.service.PvConvertRuleService;
import com.cn.xiaonuo.tcrfinance.tcrusers.entity.PvLoginUser;
import com.cn.xiaonuo.tcrfinance.tcrusers.entity.PvWallet;
import com.cn.xiaonuo.tcrfinance.tcrusers.mapper.PvLoginUserMapper;
import com.cn.xiaonuo.tcrfinance.tcrusers.mapper.PvWalletMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.Map;

import static com.cn.xiaonuo.common.constant.SysUserConstant.*;


@Service
@Slf4j
public class MerchantBackServiceImpl implements MerchantBackService {

    @Autowired
    private PvProductMapper pvProductMapper;
    @Autowired
    private PvProductRepositoryMapper pvProductRepositoryMapper;
    @Autowired
    private MerchantBackMapper merchantBackDao;
    @Autowired
    private PvDeliveryAddMapper pvDeliveryAddMapper;
    @Autowired
    private PvProductGroupMapper pvProductGroupMapper;

    @Autowired
    private PvLoginUserMapper pvLoginUserMapper;

    @Autowired
    private PvWalletMapper pvWalletMapper;

    @Autowired
    private PvProductGlMapper pvProductGlMapper;

    @Autowired
    private PvShopMapper pvShopMapper;

    @Autowired
    private ThirdStorageMapper thirdStorageMapper;

    @Autowired
    private PvConvertRuleService pvConvertRuleService;

    @Autowired
    private PvSystemConfigMapper pvSystemConfigMapper;

    /**
     * 寄售专区
     */
    private static final Integer CONSIGN = 2;

    /**
     * 订单最大价格
     */
    public static final BigDecimal ORDER_MAX_PRICE = new BigDecimal("15000");


    @Override
    @Transactional(rollbackFor = {Exception.class})
    public BusinessMessage insertProductInfo(PvProductParam pvProductParam) {
        Integer salesModeId = pvProductParam.getSalesModeId();
        if (null == salesModeId) {
            return BusinessMessage.fail("请选择销售模式");
        }
        Integer productTypeId = pvProductParam.getProductTypeId();
        if (null == productTypeId) {
            return BusinessMessage.fail("请选择商品类型");
        }
        String name = pvProductParam.getName();
        if (StringUtils.isBlank(name)) {
            return BusinessMessage.fail("商品名称不能为空");
        }
        String imageUrl = pvProductParam.getImageUrl();
        if (StringUtils.isBlank(imageUrl)) {
            return BusinessMessage.fail("商品图片不能为空");
        }
        Integer count = pvProductParam.getCount();
        if (count == null) {
            return BusinessMessage.fail("商品数量不能为空");
        }
        Integer shopId = pvProductParam.getShopId();
        if (shopId == null) {
            return BusinessMessage.fail("店铺ID不能为空");
        }
        BigDecimal price = pvProductParam.getPrice();
        if (null == price) {
            return BusinessMessage.fail("商品价格不能为空");
        }
        BigDecimal personal_price = pvProductParam.getPersonalPrice();
        if (salesModeId.intValue() == 1) {
            if (null == personal_price) {
                return BusinessMessage.fail("免税价格不能为空");
            }
        }
        Integer unLockDay = pvProductParam.getUnlockday();
        if (unLockDay == null) {
            return BusinessMessage.fail("商品分组信息不能为空");
        }

        Integer storageId = pvProductParam.getStorageId();
        if (storageId == null) {
            return BusinessMessage.fail("仓储地址不能为空");
        }


        /**
         * 只有寄售专区模式才会进入此流程
         */
        if (CONSIGN.equals(salesModeId)) {
            //总账号一对多,商家=>总账号
            synGlUser(shopId);
        }
        PvShop pvShop = pvShopMapper.selectById(shopId);
        // 同步商品
        synAddProduct(pvProductParam, pvShop, storageId);

        return BusinessMessage.success();
    }

    private void synAddProduct(PvProductParam pvProductParam, PvShop pvShop, Integer storageId) {
        String curTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        // 商品
        PvProduct product = new PvProduct();
        BeanUtil.copyProperties(pvProductParam, product);
        product.setCreateTime(curTime);
        product.setProCheckTime(new Date());
        product.setRecommend(0);
        product.setSoldOut(0);
        product.setCreatedAt(curTime);
        product.setUpdatedAt(new Date());
        product.setStcScale(new BigDecimal("0"));
        product.setOSort(Convert.toInt(pvProductParam.getOSort(), 100));
        product.setShopId(pvShop.getId());
        //积分商品默认寄售
        product.setPostage(BigDecimal.ZERO);
        product.setSalesVirtual(0);
        product.setIsSales(3);
        //仓储id
        product.setStorageId(storageId);
        product.setCyclenum(50);
        //售卖状态-正常
        product.setProdStatus(1);
        //获取最大排序
        Integer maxOSort = pvProductMapper.selectMaxOSort();
        product.setOSort(maxOSort + 1);
        pvProductMapper.insert(product);

        Integer productId = product.getId();
        //商品详情
        PvProductRepository productRepository = new PvProductRepository();
        productRepository.setShopId(pvShop.getId());
        productRepository.setProductId(productId);
        productRepository.setProductImageUrl(pvProductParam.getImageUrl());
        productRepository.setProductName(pvProductParam.getName());
        productRepository.setPrice(pvProductParam.getPrice());
        productRepository.setCount(pvProductParam.getCount());
        productRepository.setCreatedAt(curTime);
        productRepository.setUpdatedAt(new Date());
        productRepository.setPersonalPrice(pvProductParam.getPersonalPrice());
        pvProductRepositoryMapper.insert(productRepository);
        PvProductGroup pvProductGroup = pvProductGroupMapper.selectById(pvProductParam.getUnlockday());
        if (ObjectUtils.isEmpty(pvProductGroup)) {
            throw new ServiceException("商品分组信息不正确");
        }

        LambdaQueryWrapper<PvProduct> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PvProduct::getUnlockday, pvProductParam.getUnlockday());
        Integer num = pvProductMapper.selectCount(wrapper);
        pvProductGroup.setAssociatedQuantity(num);
        pvProductGroupMapper.updateById(pvProductGroup);

        //同步上限算法
        synConvertRule(product);
        //同步版本
        synSystemConfig(product);
    }

    private void synSystemConfig(PvProduct product) {
        PvSystemConfig pvSystemConfig = new PvSystemConfig();
        pvSystemConfig.setName(product.getId() + "");
        pvSystemConfig.setContent("10");
        pvSystemConfig.setRemark("同步商家后台商品：" + product.getName() + "可卖时间");
        pvSystemConfig.setUpdatedAt(new Date());
        pvSystemConfigMapper.insert(pvSystemConfig);
    }


    private void synConvertRule(PvProduct product) {
        Integer productId = product.getId();
        BigDecimal price = product.getPrice();
        //赋值
        PvConvertRule pvConvertRule = new PvConvertRule();
        pvConvertRule.setProductId(Convert.toStr(productId));
        pvConvertRule.setReleaseNum(10);
        //每天-每周-每笔
        pvConvertRule.setMaxDayNum(getMaxDayNum(price));
        pvConvertRule.setMaxWeekNum(getMaxWeekNum(price));
        pvConvertRule.setMaxEveryNum(getMaxEveryNum(price));
        pvConvertRule.setCTime(product.getCreateTime());
        pvConvertRule.setRemark(product.getName());
        //插入数据
        pvConvertRuleService.save(pvConvertRule);
    }

    private Integer getMaxWeekNum(BigDecimal price) {
        return getMaxDayNum(price) * 10;
    }

    /**
     * 每天订单上限
     *
     * @param price
     * @return
     */
    private Integer getMaxDayNum(BigDecimal price) {
        return ORDER_MAX_PRICE.divideToIntegralValue(price).intValue();
    }

    /**
     * 每笔金额
     *
     * @param price
     * @return
     */
    private Integer getMaxEveryNum(BigDecimal price) {
        return getMaxWeekNum(price) / 7;
    }

    private void synGlUser(Integer shopId) {
        if (repeatGlUser(shopId)) {
            log.info("商家对应的总账号已存在,重复添加");
            return;
        }
        // 同步用户
        Long maxPhone = Long.parseLong(pvLoginUserMapper.selectMaxPhone());
        String phoneOne = String.valueOf((maxPhone + 1));
        String phoneTwo = String.valueOf((maxPhone + 2));
        PvLoginUser pvLoginUserOne = synUser(phoneOne, DEFAULT_PASSWORD);
        PvLoginUser pvLoginUserTwo = synUser(phoneTwo, DEFAULT_PASSWORD);
        buildGlUser(pvLoginUserOne, pvLoginUserTwo, shopId);
    }

    private boolean repeatGlUser(Integer shopId) {
        LambdaQueryWrapper<PvProductGl> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PvProductGl::getShopId, shopId);
        List<PvProductGl> pvProductGlList = pvProductGlMapper.selectList(queryWrapper);
        return pvProductGlList.size() >= 1;
    }

    private PvLoginUser synUser(String phone, String password) {
        PvLoginUser pvLoginUser = new PvLoginUser() {{
            setPhone(phone);
            setPassword(password);
            setHeadAdd(DEFAULT_HEAD_IMAGE);
            setUserName(DEFAULT_USER_NAME);
        }};

        pvLoginUser.setStatus(Short.valueOf("0"));
        pvLoginUserMapper.insert(pvLoginUser);
        Integer userId = pvLoginUser.getId();
        String accountNum = ACCOUNT_PREFIX;
        if (null != userId) {
            if (userId > 0 && userId <= 9) {
                accountNum = accountNum + ("00000") + (userId);
            } else if (userId >= 10 && userId < 99) {
                accountNum = accountNum + ("0000") + (userId);
            } else if (userId >= 100 && userId < 999) {
                accountNum = accountNum + ("000") + (userId);
            } else if (userId >= 1000 && userId < 9999) {
                accountNum = accountNum + ("00") + (userId);
            } else if (userId >= 10000 && userId < 99999) {
                accountNum = accountNum + ("0") + (userId);
            } else if (userId >= 100000 && userId < 999999) {
                accountNum = accountNum + (userId);
            } else {
                accountNum = accountNum + (userId);
            }
        }
        pvLoginUser.setAccounts(accountNum);
        String nowTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        pvLoginUser.setCreateTime(nowTime);
        pvLoginUserMapper.updateById(pvLoginUser);
        //注册成功默认创建一个钱包
        PvWallet wallet = new PvWallet();
        wallet.setUserId(userId);
        wallet.setCreateTime(nowTime);
        wallet.setAvailableBalance(new BigDecimal("0.00"));
        pvWalletMapper.insert(wallet);
        pvLoginUser.setWalletId(wallet.getId());
        return pvLoginUser;
    }

    private void buildGlUser(PvLoginUser user1, PvLoginUser user2, Integer shopId) {
        //先去重
        LambdaQueryWrapper<PvProductGl> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PvProductGl::getShopId, shopId);
        List<PvProductGl> pvProductGlList = pvProductGlMapper.selectList(queryWrapper);
        if (pvProductGlList.size() > 0) {
            log.info("总账号已存在,重复添加");
            return;
        }
        //同步总账号
        PvProductGl pvProductGl = new PvProductGl();
        pvProductGl.setProdStatus(1);
        pvProductGl.setSellUserId(user1.getId());
        pvProductGl.setWalletId(user1.getWalletId());
        pvProductGl.setPoundageUserId(user2.getId());
        pvProductGl.setPoundageWalletId(user2.getWalletId());
        pvProductGl.setShopId(shopId);
        pvProductGl.setCsProfitNum(new BigDecimal("0.3"));
        pvProductGl.setPtProfitNum(new BigDecimal("0.7"));
        pvProductGl.setProfitNum(new BigDecimal("0.3"));
        pvProductGl.setVouchersNum(new BigDecimal("0.7"));
        pvProductGl.setGain(new BigDecimal("0.1"));
        // 这里固定2719
        pvProductGl.setWalletUserId(2719);
        pvProductGlMapper.insert(pvProductGl);
    }


    @Override
    public BusinessMessage selectProductInfoDetail(ProductReqParam productReqParam) {
        BusinessMessage message = new BusinessMessage();
        try {
            String productName = productReqParam.getProductName();
            Integer salesModeId = productReqParam.getSalesModeId();
            // 设置分页参数
            Page page = PageFactory.defaultPage();
            PageHelper.startPage((int) page.getCurrent(), (int) page.getSize());
            List<Map<String, Object>> resultList = merchantBackDao.selectProductInfoDetail(productName, salesModeId);
            PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(resultList);
            message.setData(pageInfo);
            message.setSuccess(true);
            message.setMessage(ResponseData.DEFAULT_SUCCESS_MESSAGE);
            message.setCode("200");
        } catch (Exception e) {
            message.setMessage("请求失败");
            message.setSuccess(false);
            message.setCode("-1");
            e.printStackTrace();
        }
        return message;
    }


    @Override
    @Transactional(rollbackFor = {Exception.class})
    public BusinessMessage updateProductInfo(PvProductParam pvProductParam) {
        BusinessMessage message = new BusinessMessage();
        try {
            Integer salesModeId = pvProductParam.getSalesModeId();
            if (null == salesModeId) {
                message.setMessage("请选择销售模式");
                message.setCode("-1");
                message.setSuccess(false);
                return message;
            }
            if (!salesModeId.equals(2)) {
                Integer productTypeId = pvProductParam.getProductTypeId();
                if (null == productTypeId) {
                    message.setMessage("请选择商品类型");
                    message.setCode("-1");
                    message.setSuccess(false);
                    return message;
                }
            }
            String name = pvProductParam.getName();
            if (StringUtils.isBlank(name)) {
                message.setMessage("商品名称不能为空");
                message.setCode("-1");
                message.setSuccess(false);
                return message;
            }
            String imageUrl = pvProductParam.getImageUrl();
            if (StringUtils.isBlank(imageUrl)) {
                message.setMessage("商品图片不能为空");
                message.setCode("-1");
                message.setSuccess(false);
                return message;
            }
            Integer count = pvProductParam.getCount();
            if (count == null) {
                message.setMessage("商品数量不能为空");
                message.setCode("-1");
                message.setSuccess(false);
                return message;
            }
            Integer shopId = pvProductParam.getShopId();
            if (shopId == null) {
                message.setMessage("店铺ID不能为空");
                message.setCode("-1");
                message.setSuccess(false);
                return message;
            }
            BigDecimal price = pvProductParam.getPrice();
            if (null == price) {
                message.setMessage("商品价格不能为空");
                message.setCode("-1");
                message.setSuccess(false);
                return message;
            }
            BigDecimal personal_price = pvProductParam.getPersonalPrice();
            if (salesModeId.intValue() == 1) {
                if (null == personal_price) {
                    message.setMessage("免税价格不能为空");
                    message.setCode("-1");
                    message.setSuccess(false);
                    return message;
                }
            }
            //修改后的分组
            Integer unlockday = pvProductParam.getUnlockday();
            if (unlockday == null) {
                message.setMessage("商品分组信息不能为空");
                message.setCode("-1");
                message.setSuccess(false);
                return message;
            }

            Integer storageId = pvProductParam.getStorageId();
            if (storageId == null) {
                return BusinessMessage.fail("仓储地址不能为空");
            }
            Integer id = pvProductParam.getId();

            //商品
            PvProduct product = pvProductMapper.selectById(id);
            //用于存放修改之前的商品分组
            Integer productUnlockday = null;
            if (null != product) {
                productUnlockday = product.getUnlockday();
                if (0 == product.getSoldOut()) {
                    BeanUtil.copyProperties(pvProductParam, product);
                    product.setUpdatedAt(new Date());
                    product.setSoldOut(1);
                    pvProductMapper.updateById(product);
                } else {
                    message.setSuccess(false);
                    message.setCode("-1");
                    message.setMessage("请先下架商品再编辑");
                    return message;
                }
            } else {
                message.setCode("-1");
                message.setMessage("商品数据为空");
                message.setSuccess(false);
                return message;
            }
            //商品详情
            LambdaQueryWrapper<PvProductRepository> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(PvProductRepository::getProductId, product.getId());
            PvProductRepository productRepository = pvProductRepositoryMapper.selectOne(lambdaQueryWrapper);
            if (productRepository == null) {
                productRepository = new PvProductRepository();
            }
            productRepository.setPersonalPrice(personal_price);
            productRepository.setPrice(price);
            productRepository.setProductImageUrl(imageUrl);
            productRepository.setCount(count);
            productRepository.setUpdatedAt(new Date());
            if (productRepository.getId() != null) {
                pvProductRepositoryMapper.updateById(productRepository);
            } else {
                String curTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
                productRepository.setShopId(shopId);
                productRepository.setProductId(product.getId());
                productRepository.setProductName(name);
                productRepository.setCreatedAt(curTime);
                pvProductRepositoryMapper.insert(productRepository);
            }
            if (productUnlockday != null) {
                if (!productUnlockday.equals(unlockday)) {
                    this.updatePvProductGroup(productUnlockday);
                }
            }
            this.updatePvProductGroup(unlockday);
            message.setCode("200");
            message.setSuccess(true);
            message.setMessage(ResponseData.DEFAULT_SUCCESS_MESSAGE);
        } catch (Exception e) {
            message.setCode("-1");
            message.setSuccess(false);
            message.setMessage("请求失败");
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            e.printStackTrace();
        }
        return message;
    }

    /**
     * 修改分组信息
     *
     * @param unlockday 分组id
     */
    private void updatePvProductGroup(Integer unlockday) {
        PvProductGroup pvProductGroup = pvProductGroupMapper.selectById(unlockday);
        if (ObjectUtils.isNotEmpty(pvProductGroup)) {
            LambdaQueryWrapper<PvProduct> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(PvProduct::getUnlockday, unlockday);
            wrapper.eq(PvProduct::getDel, 0);
            Integer count = pvProductMapper.selectCount(wrapper);
            pvProductGroup.setAssociatedQuantity(count);
            pvProductGroupMapper.updateById(pvProductGroup);
        } else {
            throw new RuntimeException();
        }
    }

    //**************************************************商家后台*********************************************************
    @Override
    public BusinessMessage selectOrderLogistics(Map<String, Object> params) {
        BusinessMessage message = new BusinessMessage();
        try {
            Integer deliveryStatus = 0;//物流状态:0.全部1:未发货2.已发货3.已收货
            if (params.get("deliveryStatus") instanceof Integer) {
                deliveryStatus = (Integer) params.get("deliveryStatus");
            }
            if (params.get("deliveryStatus") instanceof String) {
                if (StringUtils.isNotBlank((String) params.get("deliveryStatus"))) {
                    deliveryStatus = Integer.parseInt((String) params.get("deliveryStatus"));
                }
            }
            String serialNumber = null;//订单编号
            if (params.get("serialNumber") instanceof String) {
                if (StringUtils.isNotBlank((String) params.get("serialNumber"))) {
                    serialNumber = (String) params.get("serialNumber");
                }
            }
            Integer shopId = null;
            if (params.get("shopId") instanceof Integer) {
                shopId = (Integer) params.get("shopId");
            }
            if (params.get("shopId") instanceof String) {
                if (StringUtils.isNotBlank((String) params.get("shopId"))) {
                    shopId = Integer.parseInt((String) params.get("shopId"));
                }
            }
            Integer pageSize = null;
            if (params.get("pageSize") instanceof Integer) {
                pageSize = (Integer) params.get("pageSize");
            }
            if (params.get("pageSize") instanceof String) {
                if (StringUtils.isNotBlank((String) params.get("pageSize"))) {
                    pageSize = Integer.parseInt((String) params.get("pageSize"));
                }
            }
            Integer pageNum = null;
            if (params.get("pageNum") instanceof Integer) {
                pageNum = (Integer) params.get("pageNum");
            }
            if (params.get("pageNum") instanceof String) {
                if (StringUtils.isNotBlank((String) params.get("pageNum"))) {
                    pageNum = Integer.parseInt((String) params.get("pageNum"));
                }
            }
            if (null == pageNum || Integer.valueOf(pageNum) <= 1) {
                pageNum = 1;
            }
            if (null == pageSize || Integer.valueOf(pageSize) <= 1) {
                pageSize = 10;
            }
            if (0 == deliveryStatus) {
                deliveryStatus = null;
            }
            PageHelper.startPage(pageNum, pageSize);
            List<Map<String, Object>> resultList = merchantBackDao.selectOrderLogistics(deliveryStatus, serialNumber, shopId);
            if (resultList.size() >= 0) {
                PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(resultList);
                message.setData(pageInfo);
                message.setSuccess(true);
                message.setMessage(ResponseData.DEFAULT_SUCCESS_MESSAGE);
                message.setCode("200");
                return message;
            } else {
                message.setMessage("无数据");
                message.setSuccess(false);
                message.setCode("200");
                return message;
            }
        } catch (Exception e) {
            message.setMessage("请求失败");
            message.setSuccess(false);
            message.setCode("-1");
            e.printStackTrace();
        }
        return message;
    }

    @Override
    public BusinessMessage updateOrderLogisticsStatus(Map<String, Object> params) {
        BusinessMessage message = new BusinessMessage();
        try {
            Integer deliveryId = (Integer) params.get("deliveryId");
            String logisticsNum = (String) params.get("logisticsNum");
            String logisticsType = (String) params.get("logisticsType");
            PvDeliveryAdd deliveryAdd = pvDeliveryAddMapper.selectById(deliveryId);
            if (null != deliveryAdd) {
                deliveryAdd.setLogisticsNum(logisticsNum);
                deliveryAdd.setLogisticsStatus("2");
                deliveryAdd.setLogisticsType(logisticsType);
                pvDeliveryAddMapper.updateById(deliveryAdd);
                message.setSuccess(true);
                message.setMessage(ResponseData.DEFAULT_SUCCESS_MESSAGE);
                message.setCode("200");
            } else {
                message.setMessage("无数据");
                message.setSuccess(false);
                message.setCode("200");
            }
        } catch (Exception e) {
            message.setMessage("请求失败");
            message.setSuccess(false);
            message.setCode("-1");
            e.printStackTrace();
        }
        return message;
    }

    @Override
    public BusinessMessage selectTradeDetail(Map<String, Object> params) {
        BusinessMessage message = new BusinessMessage();
        try {
            Integer sellAccounts = (Integer) params.get("sellAccounts");
            Integer buyAccounts = (Integer) params.get("buyAccounts");
            String serialNumber = (String) params.get("serialNumber");
            Long beginTime = (Long) params.get("beginTime");
            Long endTime = (Long) params.get("endTime");
            Integer shopId = (Integer) params.get("shopId");
            Integer pageSize = (Integer) params.get("pageSize");
            Integer pageNum = (Integer) params.get("pageNum");
            if (null == pageNum || Integer.valueOf(pageNum) <= 1) {
                pageNum = 1;
            }
            if (null == pageSize || Integer.valueOf(pageSize) <= 1) {
                pageSize = 10;
            }
            String begin = null;
            String end = null;
            if (null != beginTime && null != endTime) {
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                begin = format.format(beginTime) + " 00:00:00";
                end = format.format(endTime) + " 23:59:59";
            }
            PageHelper.startPage(pageNum, pageSize);
            List<Map<String, Object>> resultList = merchantBackDao.selectTradeDetail(sellAccounts, buyAccounts, serialNumber, begin, end, shopId);
            if (resultList.size() > 0) {
                PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(resultList);
                message.setData(pageInfo);
                message.setSuccess(true);
                message.setMessage(ResponseData.DEFAULT_SUCCESS_MESSAGE);
                message.setCode("200");
            } else {
                message.setMessage("无数据");
                message.setSuccess(false);
                message.setCode("-1");
            }
        } catch (Exception e) {
            message.setMessage("请求失败");
            message.setSuccess(false);
            message.setCode("-1");
            e.printStackTrace();
        }
        return message;
    }

    @Override
    public BusinessMessage selectShopUserLogin(Map<String, Object> params) {
        BusinessMessage message = new BusinessMessage();
        try {
            String accounts = (String) params.get("accounts");
            String pwd = (String) params.get("pwd");
            if (null == accounts | null == pwd) {
                message.setMessage("无数据");
                message.setSuccess(false);
                message.setCode("-1");
                return message;
            }
            Map<String, Object> map = merchantBackDao.selectShopUserLogin(accounts, pwd);
            if (null != map) {
                message.setData(map);
                message.setSuccess(true);
                message.setMessage(ResponseData.DEFAULT_SUCCESS_MESSAGE);
                message.setCode("200");
                return message;
            } else {
                message.setMessage("无数据");
                message.setSuccess(false);
                message.setCode("-1");
                return message;
            }
        } catch (Exception e) {
            message.setMessage("请求失败");
            message.setSuccess(false);
            message.setCode("-1");
            e.printStackTrace();
        }
        return message;
    }

}
